<?php

// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 商品促销标签
 *
 * @package 	htdocs
 * @subpackage  quanliadmin
 * @author  	create by allen.qiang
 */

quanli_require('module/base/goods/goodsPromotion.class.php');

class ajax_goodsPromotion {

    public function execute() {

        if (isset($_REQUEST['operFlg']) && $_REQUEST['operFlg'] >0) {

            switch ($_REQUEST['operFlg']) {
                case 1:	//商品促销标签列表
                    $this->goodsPromotionList();
                    break;
                case 2:	//保存商品促销标签
                    $this->goodsPromotionSave();
                    break;
                case 3:	//获取添加时促销标签的最大序号
                    $this->maxSort();
                    break;
                case 4:	//获取修改时所需的信息
                    $this->getgoodsPromotion();
                    break;
                case 5: //删除
                    $this->goodsPromotionDel();
                    break;
                case 6:	//使用标志
                    $this->goodsPromotionChange();
                    break;
                default:
                    backendError();
            }

        } else {
            backendError();
        }

    }

    /**
     * 商品促销标签列表
     */
    private function goodsPromotionList() {

        global $G_PGLOB;

        $goodsPromotion = new goodsPromotion($G_PGLOB->DBCA);

        $goodsMaterInfo = $goodsPromotion->searchPromotionTag($_REQUEST);

        $result_array = array(
            'page'  => $_REQUEST['page'] ,
            'total' => $_REQUEST['count'] ,
            'rows'  => array()
        );

        if ($goodsMaterInfo) {

            $edit_permission = $G_PGLOB->has_permission(553);

            foreach($goodsMaterInfo as &$s_l) {

                if ($edit_permission) {
                    $result_array['rows'][] = array(
                        'id'   => $s_l['tagID'],
                        'cell' => array(
                            '<a href="javascript: void(0);" onclick="goodsPromotion.edit(' . $s_l['tagID'] . ',1)">维护图片</a>&nbsp;&nbsp;&nbsp;&nbsp;'.
                            '<a href="javascript: void(0);" onclick="goodsPromotion.edit(' . $s_l['tagID'] . ',0)">维护值</a>' ,
                            $s_l['sortOrder'],
                            $s_l['tagName'],
                            $s_l['useFlg'] == 1 ?
                                '<a href="javascript: void(0);" onclick="goodsPromotion.doAction(' . $s_l['tagID'] . ', 0) " >启用</a>' :
                                '<a href="javascript: void(0);" onclick="goodsPromotion.doAction(' . $s_l['tagID'] . ', 1 ) " >禁用</a>'
                        )

                    );
                } else {
                    $result_array['rows'][] = array(
                        'id'   => $s_l['tagID'],
                        'cell' => array(
                            $s_l['tagID'],
                            $s_l['sortOrder'],
                            $s_l['tagName'],
                            $s_l['useFlg'] == 1 ?
                                '启用' :
                                '禁用'
                        )

                    );
                }
            }
        }

        echo json_encode( $result_array );

    }

    /**
     * 保存
     */
    private function goodsPromotionSave() {

        global $G_PGLOB;

        $goodsPromotion= new goodsPromotion($G_PGLOB->DBCA);
        $retArr = $goodsPromotion->saveGoodsPromotion($_REQUEST);

        if ($retArr['code'] != 200) {
            die(json_encode(array( 'code' => $retArr['code'] , 'message' => $retArr['message'] )));
        } else {
            die(json_encode(array( 'code' => 200 , 'message' => '数据保存成功' , 'data' => $retArr['data'] )));
        }

    }

    /**
     * 获取添加时原材料的最大序号
     */
    private function maxSort() {

        global $G_PGLOB;

        $goodsPromotion= new goodsPromotion($G_PGLOB->DBCA);
        $maxSort = $goodsPromotion->maxSort() + 1;

        echo json_encode($maxSort);

    }

    /**
     * 删除
     */
    private function goodsPromotionDel() {
        global $G_PGLOB;

        //接受要删除的参数
        $tagID = $_REQUEST['p_array'];

        $goodsPromotion = new goodsPromotion($G_PGLOB->DBCA);
        $retArr = $goodsPromotion->delgoodsPromotion($tagID);

        if ($retArr['code'] != 200) {
            die(json_encode(array( 'code' => $retArr['code'] , 'message' => $retArr['message'] )));
        } else {
            die(json_encode(array( 'code' => 200 , 'message' => '商品促销标签删除成功' )));
        }
    }

    /**
     * 使用标志
     */
    private function goodsPromotionChange() {

        global $G_PGLOB;

        $tagID = $_REQUEST['tagID'];
        $useFlg = $_REQUEST['useFlg'];

        $goodsPromotion = new goodsPromotion($G_PGLOB->DBCA);
        $retArr = $goodsPromotion->changeUse($tagID, $useFlg);

        if ($retArr['code'] != 200) {
            echo json_encode(array( 'code' => $retArr['code'] , 'message' => $retArr['message'] ));
        } else {
            echo json_encode(array( 'code' => 200 , 'message' => '使用标志修改成功' ));
        }

    }

    /**
     * 获取修改时所需的信息
     */
    private function getgoodsPromotion() {

        global $G_PGLOB;

        if (!isset($_REQUEST['tagID']) || !is_numeric($_REQUEST['tagID'])) {
            die(json_encode(array( 'code' => 400 , 'message' => '需要修改的商品促销标签不存在' )));
        }

        $goodsPromotion = new goodsPromotion($G_PGLOB->DBCA);
        $goodsMaterInfo = $goodsPromotion->searchPromotionTag($_REQUEST);

        if ($goodsMaterInfo) {
            foreach ($goodsMaterInfo as $k=>$v) {
                $v['image'] = G_IMAGE_URL.$v['image'];
            }
        }

        echo json_encode($goodsMaterInfo[0]);

    }
}